<template>
  <div class="app-container">
    <div>
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
      >
        <el-form-item label="标题：" prop="title">
          <el-input v-model="searchForm.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="介绍：" prop="introduce">
          <el-input v-model="searchForm.introduce" placeholder="请输入介绍" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="fetchData(true)">查询</el-button>
          <el-button type="danger" @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div style="display: flex">
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column align="center" label="id" width="95" prop="id" />
        <el-table-column label="标题" prop="title" />
        <el-table-column label="介绍" width="180" prop="introduce" />
        <el-table-column label="创建时间" prop="create_time" />
        <el-table-column label="更新时间" prop="update_time" />
        <el-table-column label="操作" prop="op">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="handleEdit(scope.row)"
            >查看</el-button>
            <el-button
              type="danger"
              @click="handleDel(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <Pagination
      :page-sizes="paginationConfig.pageSizes"
      :layout="paginationConfig.layout"
      :total="paginationConfig.total"
      :page="paginationConfig.page"
      :limit="paginationConfig.pageSize"
      @pagination="handleChangePage"
    />
  </div>
</template>

<script>
import {
  delArticleLearningStudyDetail, delKnowledgeDetail,
  getArticleLearningStudyList,
  getKnowledgeList
} from '@/api/knowledge'

export default {
  data() {
    return {
      action: process.env.VUE_APP_BASE_UPLOAD,
      dialogImageUrl: '',
      list: null,
      listLoading: true,
      searchForm: {
        title: '',
        introduce: ''
      },
      paginationConfig: {
        direction: 'center', // 分页位置
        layout: 'total, sizes, prev, pager, next, jumper', // 分页类型
        pageSizes: [10, 20], // 分页pages
        total: 0,
        page: 1, // 页数-默认1
        pageSize: 10 // 每页条数-默认10
      },
      curryNew: {},
      dialogNewsDetailVisible: false,
      form: {
        icon: '',
        name: ''
      },
      categoryId: 0
    }
  },
  created() {
    this.fetchData(true)
  },
  methods: {
    fetchData(isClear = false) {
      if (isClear) {
        this.paginationConfig.page = 1
      }
      this.listLoading = true
      const { page, pageSize } = this.paginationConfig
      const { introduce, title } = this.searchForm
      const obj = {
        page,
        pageSize,
        introduce,
        title
      }
      getKnowledgeList({
        ...obj
      }).then((response) => {
        if (response.code === 200) {
          this.list = response.data.list
          this.paginationConfig.total = response.data.total
          this.listLoading = false
        }
      })
    },
    handleChangePage({ page, pageSize }) {
      this.paginationConfig.page = page
      this.paginationConfig.pageSize = pageSize
      this.fetchData()
    },

    onReset() {
      this.$refs.searchFormRef.resetFields()
      this.fetchData()
    },
    handleAdd() {
      this.$router.push({
        name: 'knowledgeDetail',
        query: {
          type: 'add'
        }
      })
    },
    handleEdit(e) {
      this.$router.push({
        name: 'knowledgeDetail',
        query: {
          id: e.id,
          type: 'edit'
        }
      })
    },
    async handleDel(e) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delKnowledgeDetail({
          id: e.id
        })
        if (res.code === 200) {
          this.$message.success(res.data)
          this.fetchData()
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.dialog-content {
  width: 375px;
  height: 600px;
  overflow: hidden;
  overflow-y: scroll;
}
.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px;
  height: 178px;
  display: flex;
  align-items: center;
  justify-content: center;
}
::v-deep {
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 178px;
    height: 178px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
